package com.iflytek.springboot_medicine.controller;

import com.iflytek.springboot_medicine.common.Page;
import com.iflytek.springboot_medicine.entity.Apply;
import com.iflytek.springboot_medicine.pojo.ResponseMessage;
import com.iflytek.springboot_medicine.service.impl.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/apply")
public class AppController {
    @Autowired
    private AppService appService;

//    //查询用户信息
//    @GetMapping("/select")
//    public ResponseMessage select() {
//        List<Apply> ApplyList = appService.selectUser();
//        return ResponseMessage.success(ApplyList);
//    }

    //根据id查询用户信息
    @GetMapping("/selectById/{id}")
    public ResponseMessage selectById(@PathVariable Integer id) {
        Apply user = appService.selectUserByid(id);
        return ResponseMessage.success(user);
    }

    //增加
    @PostMapping("/add")
    public ResponseMessage add(@RequestBody Apply user) {
        appService.insertUser(user);
        return ResponseMessage.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseMessage delete(@PathVariable Integer id) {
        appService.delectUser(id);
        return ResponseMessage.success();
    }

    //批量删除用户
    @DeleteMapping("/delete/batch")
    public ResponseMessage batchDelete(@RequestBody List<Integer> ids) {
        appService.delectBatch(ids);
        return ResponseMessage.success();
    }

    //修改
    @PutMapping("/update")
    public ResponseMessage update(@RequestBody Apply user) {
        appService.updateUser(user);
        return ResponseMessage.success();
    }
//    @PutMapping("/updateStatus")
//    public ResponseMessage updateStatus(@RequestParam Apply user) {
//        appService.updateStatus(user);
//        return ResponseMessage.success();
//    }

    //分页查询
    @GetMapping("/selectByPage")
    public ResponseMessage selectByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {

        // 校验 pageNum 和 pageSize
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1; // 默认第一页
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5; // 默认每页显示10条
        }
        Page<Apply> page = appService.selectByPage(pageNum,pageSize);
        //select * from user where username like '%#{username}%' and name like '%#{name}%'
        // 调用服务层方法

        // 返回结果
        return ResponseMessage.success(page);
    }

}
